class PhotosController < ApplicationController
  before_filter :login_required, :except => [:index, :show]
  
  def index
    @photos = Photo.paginate(:page => params[:page] || 1,
                            :conditions => "parent_id is null",
                            :order => "created_at desc")
  end
  
  def new
    @school = School.find(params[:school_id]) unless params[:school_id].blank?
    @event  = Event.find(params[:event_id]) unless params[:event_id].blank?
    @student = Student.find(params[:student_id]) unless params[:student_id].blank?
    
    @photos = Photo.new
    @photos.school_id = params[:school_id] unless params[:school_id].blank?
    @photos.event_id  = params[:event_id] unless params[:event_id].blank?
    @photos.student_id = params[:student_id] unless params[:student_id].blank?
  end
  
  def create
    #@photos = Photo.new
    @school_id = params[:photos][:school_id] unless params[:photos][:school_id].blank?
    @event_id  = params[:photos][:event_id] unless params[:photos][:event_id].blank?
    @student_id = params[:photos][:student_id] unless params[:photos][:student_id].blank?
    flag = true
    1.upto(Photo.upload_amount) do |index|
      flag = false unless params[:photos]["no_#{index}"].blank?
    end
    if flag
      flash[:notice] = "你没有选择任何图片"
      new()
    end
    @upload_photos = []
    1.upto(Photo.upload_amount) do |index|
      unless params[:photos]["no_#{index}"].blank?
        photo = Photo.new(:uploaded_data => params[:photos]["no_#{index}"])
        photo.user = current_user
        photo.school_id = @school_id
        photo.event_id  = @event_id
        photo.student_id = @student_id
        photo.save!
        @upload_photos << photo
      end
    end
    
    render :action => "medit"
  end
  
  def medit
  end
  
  def mupdate
    0.upto(Photo.upload_amount-1) do |index|
      id = params[:photos]["id_#{index}"]
      unless id.blank?
        photo = Photo.find(id)
        photo.update_attributes!(:title => params[:photos]["title_#{index}"],
                                 :description => params[:photos]["description_#{index}"])
      end
    end
    unless params[:photos][:school_id].blank?
      redirect_to school_url(params[:photos][:school_id])
    else
      unless params[:photos][:event_id].blank?
        redirect_to event_url(params[:photos][:event_id])
      else
        unless params[:photos][:student_id].blank?
          student = Student.find(params[:photos][:student_id])
          redirect_to student_url(student.school_id, student.id)
        else
          redirect_to home_url
        end
      end
    end
  end
  
  def edit
    @photo = Photo.find(params[:id])
    @photo.editable_by?(self.current_user) || access_denied
  end
  
  def update
    @photo = Photo.find(params[:id])
    @photo.editable_by?(self.current_user) || access_denied
    if @photo.update_attributes!(params[:photo])
      redirect_to photo_url(@photo)
    else
      render :action => 'edit'
    end
  end
  
  def destroy
    @photo = Photo.find(params[:id])
    @photo.editable_by?(self.current_user) || access_denied
    @photo.destroy
    flash[:notice] = "你删除了标题为#{@photo.title}的照片"
    redirect_back_or_default(photos_url)
  end
  
  def show
    @photo = Photo.find(params[:id], :include => :user)
    @comments = @photo.comments
    @comment = PhotoComment.new
  end

  def share_event
    @photo = Photo.find(params[:id])
    @photo.editable_by?(self.current_user) || access_denied
    @events = current_user.events
  end
  
  def share_school
    @photo = Photo.find(params[:id])
    @photo.editable_by?(self.current_user) || access_denied
    @schools = current_user.visited_schools
  end
  
  def push_event
    @photo = Photo.find(params[:id])
    @event = Event.find(params[:event_id])
    @photo.update_attributes!(:event_id => @event.id)
    flash[:notice] = "您将自己的照片分享到了#{@event.title}"
    redirect_back_or_default(user_path(current_user))
  end
  
  def push_school
    @photo = Photo.find(params[:id])
    @school = School.find(params[:school_id])
    @photo.update_attributes!(:school_id => @school.id)
    flash[:notice] = "您将自己的照片分享到了#{@school.title}"
    redirect_back_or_default(user_path(current_user))
  end
end
